<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small"  :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目编码"  prop="projectNo">
        <el-input v-model.trim="queryParams.projectNo" @change="handleQuery"  placeholder="请输入项目编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称"  prop="productName">
        <el-input v-model.trim="queryParams.productName" @change="handleQuery" placeholder="请输入产品名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务编号" prop="taskNo">
        <el-input v-model.trim="queryParams.taskNo" @change="handleQuery"  placeholder="请输入任务编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否超期" prop="overdue">
        <el-select v-model="queryParams.overdue" filterable clearable  @change="handleQuery"  >
          <el-option v-for="dict in dict.type.project_task_overdue" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="productType">
        <el-select v-model="queryParams.productType" filterable clearable  @change="handleQuery" >
          <el-option v-for="dict in dict.type.product_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="产品线" prop="productLine">
        <el-select v-model="queryParams.productLine" filterable clearable  @change="handleQuery" >
          <el-option v-for="dict in dict.type.product_line" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search"   @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-radio-group v-model="activeName" @change="handleClick">
      <el-radio-button :label="0" >{{ this.unDoneNum }}</el-radio-button>
      <el-radio-button :label="1" >{{ this.rejectNum }}</el-radio-button>
      <el-radio-button :label="2" >{{ this.doingNum }}</el-radio-button>
      <el-radio-button :label="3" >{{ this.doneNum }}</el-radio-button>
    </el-radio-group>


     <el-table border v-loading="loading" :data="taskList" :border="false"  header-cell-class-name="table_header_row">

      <el-table-column label="超期天数"  align="center" prop="overdue">
        <template slot-scope="scope">
          <el-tag  type="danger"  v-if="scope.row.overdue > 0">  {{scope.row.overdue}} 天</el-tag>
          <el-tag  v-else>未超期</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务编号" align="center" prop="taskNo" width="150"/>
      <el-table-column label="项目编码" align="left"  width="150"  prop="projectNo">
        <template slot-scope="scope">
          <a style="color: cornflowerblue;" @click="openViewDetail(scope.row)">{{ scope.row.projectNo }}</a>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" width="150"/>
      <el-table-column label="产品名称" align="center" prop="productName" width="150"/>
      <el-table-column label="项目经理" align="center" prop="projectManagerName" />
      <el-table-column label="任务名称" align="center" prop="nodeName" width="300"/> 
      <el-table-column label="OA单号" align="center" prop="oaNo"   width="150">
        <template  slot-scope="scope">
          <a style="color: cornflowerblue;"   @click="handleSignToOA(scope.row)">{{ scope.row.oaNo }}</a> 
        </template>
      </el-table-column>
      <el-table-column label="产品类型" :show-overflow-tooltip="true" align="center" prop="productType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_type" :value="scope.row.productType" />
        </template>
      </el-table-column>
      <el-table-column label="产品线" :show-overflow-tooltip="true" align="center" prop="productLine">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_line" :value="scope.row.productLine" />
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime"  width="100"/>
      <el-table-column label="结束时间" align="center" prop="endTime"  width="100" />
      <el-table-column label="是否完成" :show-overflow-tooltip="true" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_task_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="nickName"   width="150"/>
      <el-table-column label="完成时间" align="center" prop="workEndTime"  width="100"/>
      <el-table-column label="用时" align="center" prop="dayNum"  width="100"/>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="220">
        <template slot-scope="scope">
           <el-button size="small"  type="success"      @click="handleCreateOA(scope.row)" v-show="scope.row.status == 0">发起OA</el-button>
           <el-button size="small"  type="primary" v-hasPermi="['projectManage:list:taskBindPLM']"   @click="handleRePLMFile(scope.row)" v-show="scope.row.status == 0">关联PLM文档</el-button> 
           <el-button size="small"  type="warning"   icon="el-icon-edit"  @click="handleCreateOAEdit(scope.row)" v-show="scope.row.type  == 10 && scope.row.status == 10 && scope.row.oaUrl == null">文档变更</el-button>
           <el-button size="small"  type="warning"   icon="el-icon-edit"  @click="handleRePLMFile(scope.row)" v-show="scope.row.type  == 20 && scope.row.status == 10">文档变更</el-button> 
           <el-button size="small"  type="primary"   icon="el-icon-paperclip"  @click="handleCreateOA(scope.row)" v-show="scope.row.status == 2">重新发起OA</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

     
    <el-dialog :title="title" :visible="open" v-loading="dialogLoading" @close="closeOpen" width="900px"  :close-on-click-modal="false" :close-on-press-escape="false"
      append-to-body>
       
        <el-form ref="form" :model="form"  :rules="rules">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark"  style="width:500px" ></el-input>
          </el-form-item>

          <el-form-item label="附件" prop="attachment">
                
                <div v-show="uploadFlag" style="min-width:10px">
                  <el-upload
                  class="upload-demo"
                  :action="upload.url"
                  :on-progress="handleUploadProgress"
                  :on-success="handleUploadSuccess"
                  :headers="upload.headers"
                  :file-list="fileList">
                  <el-button size="small" type="primary">点击上传</el-button> 
                  <div slot="tip" class="el-upload__tip">上传文件大小不超过10GB</div>
                </el-upload>
                </div>
                
                <div  v-show="relationFileFlag">
                  <el-row>
                    <el-col :span="10"><el-button size="small" icon="el-icon-share" type="primary" @click="relationFile" plain>关联已上传文件</el-button></el-col>
                    <el-col :span="14">{{ this.fileItem.fileName }}<el-link><i class="el-icon-close el-icon--right" v-show="this.fileItem.fileName!=null" @click="delFile"></i> </el-link></el-col>
                  </el-row>
                  
                  
                </div>  
             
            
          </el-form-item>
          
          
        </el-form> 
        <span slot="footer" class="dialog-footer" > 
          <el-button  @click="closeOpen">取 消</el-button>
          <el-button type="primary" @click="onSubmit">提 交</el-button>
        </span>  
    </el-dialog>

     

<el-dialog title="选择附件" :visible="visible"  @close="fileCloseOpen" width="700px"  :close-on-click-modal="false" :close-on-press-escape="false"
      append-to-body>

      <el-form :model="fileQueryParams" ref="fileQueryForm" size="small"  :inline="true"   label-width="100px">
        <el-form-item label="产品名称"  prop="productName">
          <el-input v-model.trim="fileQueryParams.productName"  placeholder="请输入产品名称" clearable
            @keyup.enter.native="queryMyFiles" />
        </el-form-item>
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model.trim="fileQueryParams.nodeName"   placeholder="请输入节点名称" clearable
            @keyup.enter.native="queryMyFiles" />
        </el-form-item>
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model.trim="fileQueryParams.fileName"   placeholder="请输入文件名称" clearable
            @keyup.enter.native="queryMyFiles" />
        </el-form-item>
  
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryMyFiles">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
         
      <el-table border v-loading="fileLoading" :data="fileTableData" @row-click="handleCurrentChange">
        <el-table-column label="选择" width="80">
          <template slot-scope="scope">
              <el-radio class="radio" :label="scope.row.fileId" >{{""}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column label="项目编号" align="center" prop="projectNo"  width="150"/>
        <el-table-column label="产品名称" align="center" prop="productName" width="150"/>
        <el-table-column label="节点名称" align="center" prop="nodeName" width="200"/>
        <el-table-column label="文件名称" align="left"  width="150"  prop="fileName"/> 
                
      </el-table>
      <pagination v-show="fileTotal > 0" :total="fileTotal" :page.sync="fileQueryParams.pageNum" :limit.sync="fileQueryParams.pageSize"
          @pagination="queryMyFiles" /> 
</el-dialog>

    <!--绑定PLM文件弹框-->
    <bind-plm-file ref="bindPlmFile"></bind-plm-file>

  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import {getTaskList,processAdd,queryTaskIndex,checkTaskStart,getMyProjectFile}  from "@/api/projectManage/task";
import {signLoginToEkp,signLoginToEkpUrl}  from "@/api/oa/oa";
import bindPlmFile  from "@/components/projectManage/list/bindPLMFile";

export default {
  name: "TaskList",
  components: { bindPlmFile },
  dicts: ['project_task_overdue','project_task_status','product_type','product_line'],
  data() {
    return {
      form:null,
      open:false,
      activeName:'0',
      unDoneNum:'',
      doingNum:'',
      doneNum:'',
      rejectNum:'',
      visible:false,
      fileLoading:false,
      uploadFlag:true,
      relationFileFlag:true,
      fileTableData:[],
      fileQueryParams:{
        pageNum:1,
        pageSize:10
      },
      fileTotal:0,
      fileList:[],
      // 遮罩层
      loading: false,

      //弹出框遮盖层
      dialogLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //项目列表
      taskList: [],
      // 弹出层标题
      title: "",
      //行号
      lineNo:0,
      //关联PLM文件
      openPLMFile:false,
      dialogPLMLoading:false,
      plmList:[],
      queryParamsPlm:{},
      //选择的PLM文件列表
      selectPLMFiles:[],  
      //当前行
      currRow:{},

      upload: {
        headers: { Authorization: "Bearer " + getToken() },
        url: process.env.VUE_APP_BASE_API + '/business/sys-files/upload/other'
      },
      fileItem:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectNo: null,
        taskNo: null,
        overdue:null,
        flag:0,
        userFlag:1,
        productType:null,
        productLine:null
      },
      form:{
        remark:null,
        fileIds:[],
        projectNodeMemberId:null
      },
      param:{},
      rules:{
        attachment: [
            { required: true, message: "附件不能为空", trigger: "blur" }
          ] 
        } 
    };
  },

 
  created() {
     
    if(this.$route.query.projectNo!=null ){
      this.queryParams.projectNo = this.$route.query.projectNo;
    }


    this.getList();
    this.getListNum();

    this.queryParams.flag = 0;
 
  },
 
  methods: {

    //关联PLM文件
    handleRePLMFile(row){
      this.$refs.bindPlmFile.open(row); 
    },


    //删除文件
    delFile(){
      this.form.fileIds = [];
      this.fileItem.fileName=null;
    },
    
    //附件关联
    handleCurrentChange(row){
      this.form.fileIds = [row.fileId]; 
      this.fileItem = row;
      this.uploadFlag = false;
 
      this.visible = false;

    },

    //关联附件弹框
    relationFile(){
        this.visible = true;
        this.queryMyFiles();
    },


    //查询已上传的文件
    queryMyFiles(){
      getMyProjectFile(this.fileQueryParams).then(response=>{
        this.fileTableData = response.rows;
        this.fileTotal =   response.total;
      })
    },


    //加载状态数量
    getListNum(){

      this.queryParams.flag = 0 ;
      getTaskList(this.queryParams).then(response => { 
          this.unDoneNum ='未完成('+ response.total+')';
          
      });


      this.queryParams.flag = 1 ;
      getTaskList(this.queryParams).then(response => { 
          this.rejectNum ='驳回('+ response.total+')'; 
      });
      
      this.queryParams.flag = 2 ;
      getTaskList(this.queryParams).then(response => { 
          this.doingNum ='审核中('+ response.total+')'; 
      });

      this.queryParams.flag = 3 ;
      getTaskList(this.queryParams).then(response => { 
          this.doneNum ='已完成('+ response.total+')'; 
      });
    },
 


    //单点登录到OA
    handleSignToOA(row){
      signLoginToEkp(row.oaId).then(response=>{
          if(response.code == 200){
            window.open(response.data.authorizationURL, '_blank');
          }else{
            this.$alert(response.data.msg, '错误');
          }  
        })
    },

    //切换状态
    handleClick(obj){  
        this.queryParams.flag = obj;
        this.getList();
    },


    //文件上传成功回调
    handleUploadSuccess(response, file, fileList){
      this.form.fileIds.push(response.data.id);
      this.fileList = fileList;
      this.dialogLoading = false;
    },


    //文件上传中会回调
    handleUploadProgress(){
        this.dialogLoading = true;
        this.relationFileFlag = false;
    },

    //提交
    onSubmit(){
       if(this.form == null || this.form.fileIds == null || this.form.fileIds.length == 0){
         this.$alert('附件不能为空!', '提示');
         return false;
       }

      this.dialogLoading = true;
      processAdd(this.form).then(response => {
        this.getList(); 
        this.getListNum();
        this.dialogLoading = false;
        this.closeOpen();
      });
    },
 

    //项目详情
    openViewDetail(row){
      row.id = row.projectListId;
      this.$nextTick(() => {
        this.$router.push({
          path: "ProjectDetail",
          query: row
        })
      })

    },
 
 
    /** 查询项目列表 */
    getList() {
      this.queryParams.exportFlag=null
      this.loading = true;
      getTaskList(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
 
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
 
     
 
    /** 发起OA */
    handleCreateOA(row) {
      this.$modal.loading("请稍候...");

      //先判断是否已经推送到OA 或者是否已经重新推送过了
      checkTaskStart(row).then(response=>{
        this.$modal.closeLoading();
          
          this.createOA(row);
        
      }).catch(()=>{
        this.$modal.closeLoading();
        this.$alert('状态错误!', '错误', {
              confirmButtonText: '确定'
          });
        this.getList(); 
        this.getListNum();  
      })
         
    },


    createOA(row){
       //如果多人任务 则判断是否是第一个 如是第一个则发起流程  非第一个则单点登录到OA
       queryTaskIndex(row.id).then(response=>{
      
              if(response.data.length == 1){
                let temp = response.data[0];
                  
                if(!temp.oaUrl){
                    this.form.type=0;
                    this.openCreateOA(row); 
                }else{
                  
                  //单点登录到OA
                  let url = temp.oaUrl.substr(temp.oaUrl.indexOf("&"),temp.oaUrl.length)+"&projectNodeMemberId="+row.id+"&projectNo="+row.projectNo+"&projectName="+row.projectName+"&productName="+row.productName+"&projectRemark="+row.projectRemark
                  
                  signLoginToEkpUrl(temp.oaUrl).then(response=>{
                    if(response.code == 200){
                  
                        window.open(response.data.authorizationURL+url, '_blank');
                      }else{
                        this.$alert(response.data.msg, '错误');
                      }  
                  })
                }


                
            }else{
              let index=0;
              let  temp;
              for(let i=0;i<response.data.length;i++){
                  let item= response.data[i];
                  if(item.id == row.id){
                  index = i;
                  temp = item;
                  break;
                  }
              }
              //如果是多个人共同完成的任务，第一个人完成任务  也是发起OA  
              if(index == 0){
                if(!temp.oaUrl){
                    this.form.type=0;
                    this.openCreateOA(row); 
                }else{
                  
                  //单点登录到OA
                  let url = temp.oaUrl.substr(temp.oaUrl.indexOf("&"),temp.oaUrl.length)+"&projectNodeMemberId="+row.id+"&projectNo="+row.projectNo+"&projectName="+row.projectName+"&productName="+row.productName+"&projectRemark="+row.projectRemark
                  
                  signLoginToEkpUrl(temp.oaUrl).then(response=>{
                    if(response.code == 200){
                  
                        window.open(response.data.authorizationURL+url, '_blank');
                      }else{
                        this.$alert(response.data.msg, '错误');
                      }  
                  })
                }
              }else{//否则单点登录到OA

                //先判断上一个节点是否已经完成
                let temp = response.data[index-1]
                if(temp.status < 5){
                  this.$alert('此任务需多人完成，上一责任人还未发起OA，请等待OA代办消息，在OA审核完后，此任务会自动完成！', '提示', {
                    confirmButtonText: '确定'
                  });
                }else{
                    //单点到OA
                    signLoginToEkp(temp.oaId).then(response=>{
                      if(response.code == 200){
                        window.open(response.data.authorizationURL, '_blank');
                      }else{
                        this.$alert(response.data.msg, '错误');
                      }  
                    })

                }  


              }

            } 
        })
    },



    /** 文档变更 */
    handleCreateOAEdit(row) {

      this.form.type=1;
      this.openCreateOA(row);
    },

    openCreateOA(row){
      this.title = '发起OA';
      this.form.projectNodeMemberId = row.id;
      this.open = true;
      this.relationFileFlag = true;
      this.uploadFlag = true;
    },
    
    


    closeOpen(){
      this.open = false;
      this.reset();
    },

    reset(){
      this.form.remark = null;
      this.form.projectNodeMemberId = null;
      this.form.fileIds = [];
      this.fileList  = [];
 
    },
    fileCloseOpen(){
      this.visible = false;
      this.form.fileIds = [];
      this.fileList  = [];
    }

  }
};
</script>
<style scoped>
.el-col {
  height: 60px;
}

.labelword ::v-deep .el-form-item__label {
  white-space: pre-line !important;
  line-height: 15px !important;
}

.el-table .danger-row {
  background: rgb(187, 179, 184);
}

.el-table .success-row {
  background: #00FF00;
}


::v-deep .table_header_row{
  background-color:#ffffff !important;

}
</style>
